<template>
  <div>
    <div class="img-btn" v-show="!isUpload">
      <i class="iconfont icon-shouqi"></i>
      <span>上传照片</span>
    </div>
    <img ref="imgimg" :src="imgUrl" alt="" v-show="isUpload" />
    <input
      ref="imgInput"
      class="img-input"
      type="file"
      accept=".jpg,.gif,.png,.bmp"
      @change="uploadImg($event)"
    />
    <!-- <el-form class="form-wrapper" ref="myForm" :model="myForm" label-width="110px">
        <el-form-item label="滑块验证：">
            <div><slider ref="slider"></slider></div>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="toSubmit">提交</el-button>
        </el-form-item>
    </el-form> -->
  </div>
  <!-- 验证条代码 -->
  <!-- 将slider验证条作为主键传入index页面 -->
</template>
<script>
// 验证条的代码
import slider from "../pages/slider";
export default {
  data() {
    return {
      myForm: {},
      isUpload: false,
      imgUrl: "",
    };
  },
  components: {
    slider,
  },
  methods: {
    uploadImg(e) {
      // console.log(e.target.files);
      this.isUpload = true;
      let file = e.target.files[0];
      let url = "";
      var reader = new FileReader();
      reader.readAsDataURL(file);
      let that = this;
      reader.onload = function (e) {
        url = this.result.substring(this.result.indexOf(",") + 1);
        that.imgUrl = "data:image/png;base64," + url;
        // that.$refs['imgimg'].setAttribute('src','data:image/png;base64,'+url);
      };
    },
    toSubmit() {
      if (this.$refs["slider"]) {
        // 未通过验证时，提示错误信息并返回
        if (!this.$refs["slider"].confirmSuccess) {
          this.$message.error("请拖动滑块验证");
          return;
        }
      }
      // 通过验证后提交
      // TO DO ...
      this.$message.success("验证成功，可提交");
    },
  },
};
</script>
<style  scoped>
/* 验证条样式 */
.form-wrapper {
  margin-top: 50px;
  width: 500px;
  margin: auto;
}
</style>